//
//  PatientInfoViewController.m
//  Nurse
//
//  Created by Sunny on 13-12-16.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "PatientInfoViewController.h"
#import "AdviceController.h"
#import "AssayController.h"

#import "PersonalDutyViewController.h"
#import "SignsViewController.h"
#import "NursingdocumentsViewController.h"
#import "PersonalTurnViewController.h"
#import "PatientInfoScroll.h"
#import "AddSignsViewController.h"
#import "PatientService.h"
#import "ScanService.h"
#import "NurseService.h"
#import "MediaController.h"

@interface PatientInfoViewController ()

@property(nonatomic,assign)IBOutlet PatientInfoScroll *scInfo;
@property(nonatomic,strong)NSString* scanzyh;
-(void)onReceiveScanData:(NSNotification*)notification;
@end

@implementation PatientInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(onReceiveScanData:) name:@"PatientInfoView" object:nil];
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //#import "PatientService.h"
    NSDictionary* patient = [[PatientService sharedInstance]currentPatient];
    
  // [[PatientService sharedInstance]currentPatient]=@"123456";
    
    self.pInfoView.txtPatientName.text = [patient valueForKey:@"姓名"];
    self.pInfoView.txtPatientID.text = [NSString stringWithFormat:@"住院号:%@",[patient valueForKey:@"住院号"]];
    self.pInfoView.txtBedNo.text = [NSString stringWithFormat:@"床位号:%@",[patient valueForKey:@"床位号"]];
    NSString*xb= [patient valueForKey:@"性别"];
    if ([xb isEqualToString:@"女"]) {
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-female2.png"];

    }
    else{
    self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-male2.png"];
    }
    //
//     NSDictionary* patient = [[PatientService sharedInstance]findPatientByPID:@"123456"];
//    [PatientService sharedInstance].currentPatient = patient;
    
    [_scInfo loadWithRowNum:5 totalNum:12];
    
    //[self.Yzbutton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
}


-(void)onReceiveScanData:(NSNotification*)notification{
    //-(void)decoderDataReceived:(NSString *)smdata{
    
        NSDictionary* userinfo = [notification userInfo];
        NSString* data = [userinfo valueForKey:@"data"];
    
        
        //NSString *topViewController = NSStringFromClass([self.navigationController.topViewController class]);
        
        //[[DataEngine sharedInstance]showMessage:topViewController withTitle:visibleViewController];
        
        if ([data length] ==9) {
            // [[DataEngine sharedInstance]showMessage:@"没有找到此条码对应的病人" withTitle:@"扫描错误1"];
            NSArray* brarray = [data componentsSeparatedByString:@"|"];
            if ([brarray count] ==2) {
                //[[DataEngine sharedInstance]showMessage:@"没有找到此条码对应的病人" withTitle:@"扫描错误2"];
                self.scanzyh = [brarray objectAtIndex:1];
                NSDictionary* patient = [[PatientService sharedInstance]currentPatient];
                NSString* patientid=[patient valueForKey:@"住院号"];
                //[[DataEngine sharedInstance]showMessage:zyh withTitle:patientid];
                if(![self.scanzyh isEqualToString: patientid]){
                    UIAlertView* alterView = [[UIAlertView alloc] initWithTitle:@"是否切换病人!"
                                            message:[NSString stringWithFormat:@"当前病人住院号:%@\n扫描条码住院号:%@",patientid,self.scanzyh]
                                                delegate:self
                                                cancelButtonTitle:@"取消"
                                                otherButtonTitles: @"确定",nil];
                    [alterView show];
                    
//                    [[DataEngine sharedInstance]showMessage:data withTitle:zyh];
//                    AddSignsViewController*vc= [[AddSignsViewController alloc]init];
//                    [self.navigationController pushViewController:vc animated:YES];
                
                }
//                else
//                {
//                    AddSignsViewController*vc= [[AddSignsViewController alloc]init];
//                    //[vc setTitle:@"体征"];
//                    //vc.Twdlx=@
//                    [self.navigationController pushViewController:vc animated:YES];
//                }
                
        }
        
        
        
    }
    
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    //[[DataEngine sharedInstance]showMessage:buttonTitle withTitle:@"test"];
    if([buttonTitle isEqualToString:@"确定"])
    {
        if (!(self.scanzyh==nil)) {
         NSDictionary* patient = [[PatientService sharedInstance]findPatientByPID:self.scanzyh];
        if(!(patient ==nil)){
        [PatientService sharedInstance].currentPatient = patient;
        self.pInfoView.txtPatientName.text = [patient valueForKey:@"姓名"];
        self.pInfoView.txtPatientID.text = [NSString stringWithFormat:@"住院号:%@",[patient valueForKey:@"住院号"]];
        self.pInfoView.txtBedNo.text = [NSString stringWithFormat:@"床位号:%@",[patient valueForKey:@"床位号"]];
            NSString*xb= [patient valueForKey:@"性别"];
            if ([xb isEqualToString:@"女"]) {
                self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-female2.png"];
                
            }
            else{
                self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-male2.png"];
            }
            [_scInfo loadWithRowNum:5 totalNum:12];
        }
            else
            {
                [[DataEngine sharedInstance]showMessage:@"没有找到此条码对应的病人" withTitle:@"扫描错误"];
            }
        }
    }
  
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)adviceClick:(id)sender{
    AdviceController *vc = [[AdviceController alloc] initWithNibName:@"AdviceController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(IBAction)assayClick:(id)sender{
    AssayController *vc = [[AssayController alloc] initWithNibName:@"AssayController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

//任务
-(IBAction)dutyClick:(id)sender{
    
    PersonalDutyViewController *vc = [[PersonalDutyViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

}

//体征
-(IBAction)bodyClick:(id)sender{
    
//    SignsViewController *vc = [[SignsViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
//    
    
   
    //[vc setTitle:@"体征"];
    //vc.Twdlx=@
    NSString *tj=@"体温单类型";
    [[NurseService sharedInstance]querySZ:tj withSuccessHandler:^(id result) {
        if ([result isKindOfClass:[NSArray class]]) {
            //[this.table reloadData];
            id record = [result objectAtIndex:0];
            AddSignsViewController*vc= [[AddSignsViewController alloc]init];
            vc.Twdlx=[record valueForKey:@"设置属性"];
             [self.navigationController pushViewController:vc animated:YES];
        }
    } andFailHandler:^(NSError *error, NSString *title) {
        [[DataEngine sharedInstance]showMessage:[error description] withTitle:@"访问设置错误"];
    }];

   
    
 
    
}

//护理文书
-(IBAction)bookClick:(id)sender{
    
    NursingdocumentsViewController *vc = [[NursingdocumentsViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
//流转
-(IBAction)turnClick:(id)sender{
    
    PersonalTurnViewController *vc = [[PersonalTurnViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

/**
 *  媒体库点击
 *
 *  @param sender <#sender description#>
 */
-(IBAction)mediaClick:(id)sender{
    
    MediaController *vc = [[MediaController   alloc] initWithNibName:@"MediaController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


@end
